package org.boulderfoodrescue.webapp.client.pickuptimes;

import java.util.List;

import org.boulderfoodrescue.webapp.client.donors.DonorRecipientInterface;
import org.boulderfoodrescue.webapp.client.events.OneTimeEventUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.OneTimeEventUpdatedHandler;
import org.boulderfoodrescue.webapp.client.events.RecurringEventUpdatedEvent;
import org.boulderfoodrescue.webapp.client.events.RecurringEventUpdatedHandler;
import org.boulderfoodrescue.webapp.client.managers.TimeSlotManager;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.OneTimeEvent;
import org.boulderfoodrescue.webapp.shared.RecurringEvent;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.FlowPanel;

public class TimeSlotView extends FlowPanel implements TimeSlotInterface{

	private TimeSlotsContentView timeSlotContentView;
	private DonorRecipientInterface donorRecipientInterface;
	private EventBus eventBus;

	public TimeSlotView(DonorRecipientInterface donorRecipientInterface, EventBus eventBus, Location location)
	{
		this.eventBus = eventBus;
		this.donorRecipientInterface = donorRecipientInterface;
		timeSlotContentView = new TimeSlotsContentView(this, location);
		this.add(timeSlotContentView);
		bind();

		List<RecurringEvent> rEvents = TimeSlotManager.getRecurringTimeSlots(location);
		List<OneTimeEvent> oEvents = TimeSlotManager.getOneTimeTimeSlots(location);
		for(RecurringEvent re : rEvents){
			timeSlotContentView.addRecurringEvent(re);
		}
		for(OneTimeEvent oe : oEvents){
			timeSlotContentView.addOneTimeEvent(oe);
		}

		//this.setAnimationEnabled(true); 
	}

	private void bind(){
		eventBus.addHandler(OneTimeEventUpdatedEvent.getType(), 
				new OneTimeEventUpdatedHandler(){

			@Override
			public void onOneTimeEventAdded(
					OneTimeEventUpdatedEvent event) {
				timeSlotContentView.addOneTimeEvent(event.getValue());
			}

			@Override
			public void onOneTimeEventUpdated(
					OneTimeEventUpdatedEvent event) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onOneTimeEventRemoved(
					OneTimeEventUpdatedEvent event) {
				// TODO Auto-generated method stub

			}

		});
		eventBus.addHandler(RecurringEventUpdatedEvent.getType(), 
				new RecurringEventUpdatedHandler(){

					@Override
					public void onRecurringEventAdded(
							RecurringEventUpdatedEvent event) {
						timeSlotContentView.addRecurringEvent(event.getValue());
					}

					@Override
					public void onRecurringEventUpdated(
							RecurringEventUpdatedEvent event) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onRecurringEventRemoved(
							RecurringEventUpdatedEvent event) {
						// TODO Auto-generated method stub
					}
		});
	}

	@Override
	public void addOneTimeSlot(OneTimeEvent event) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onBackClicked() {
		donorRecipientInterface.onTimeStotViewClose();
	}

	@Override
	public void addRecurringEvent(RecurringEvent event) {
		// TODO Auto-generated method stub

	}

}
